/* api */
import { getRobotApp, updateIntelligentQAPromptWord, updateRobotAppIntelligentQAModule, updateRobotAppSetting } from '@src/api/AIApi';
/* model */
import {
  DefaultRobotAppItem,
  SettingGPTRobotAppIntelligentQASettingTypeEnum
} from '@src/modules/setting/gpt/model'
import MsgModel from '@model/MsgModel';
/* api */
import { 
  GetGPTRobotAppModel, 
  UpdateIntelligentQAPromptWordModel, 
  UpdateRobotAppIntelligentQASettingModel, 
  UpdateRobotAppSettingModel 
} from '@model/param/in/AI';
/* hooks */
import { useLoading } from '@hooks/useLoading';
import { useFetchSettingGPTRobotOutsideModuleList } from '@gpt/hooks/edit';
import { useInitXiaoBaoChat } from '@gpt/hooks/robot';
/* vue */
import { computed, Ref, ref } from 'vue'
/* utils */
import qs from 'qs';
import { message } from '@src/util/message';
import {
  transformRobotOutsideAppToOutsideModule,
  transformRobotAppServerItemToComponentItem
} from '@src/modules/setting/gpt/utils';
/* type */
import { AIChatRobotAppItemComponentType, SettingGPTRobotAppItem } from '@src/modules/setting/gpt/types';

function useSettingGPTRobotAppIntelligentQA() {
  
  const {
    initChat
  } = useInitXiaoBaoChat()
  
  const {
    robotId,
    robotAppId
  } = useSettingGPTRobotAppEditStatus()
  
  const {
    loading: fetchGptRobotAppLoading,
    fetchGptRobotApp,
    gptRobotApp
  } = useFetchSettingGPTRobotApp()
  
  const {
    fetchRobotOutsideModuleList,
    outsideModuleList
  } = useFetchSettingGPTRobotOutsideModuleList()
  
  const {
    loading: fetchUpdateRobotAppIntelligentQAModuleLoading,
    isSuccess: fetchUpdateRobotAppIntelligentQAModuleSuccess,
    fetchUpdateRobotAppIntelligentQAModule
  } = useFetchUpdateRobotAppIntelligentQAModule()
  
  const {
    loading: fetchUpdateIntelligentQAPromptWordLoading,
    isSuccess: fetchUpdateIntelligentQAPromptWordSuccess,
    fetchUpdateIntelligentQAPromptWord
  } = useFetchUpdateIntelligentQAPromptWord()
  
  const loading = computed(() => {
    return (
      fetchGptRobotAppLoading.value 
      || fetchUpdateRobotAppIntelligentQAModuleLoading.value
      || fetchUpdateIntelligentQAPromptWordLoading.value
    )
  })
  
  const allOutsideModules = computed(() => {
    return transformRobotOutsideAppToOutsideModule(outsideModuleList.value)
  })
  
  const onFetchGptRobotAppHandler = () => {
    return fetchGptRobotApp({
      id: robotAppId.value,
      robotId: robotId.value
    })
  }
  
  const initialize = () => {
    fetchRobotOutsideModuleList()
    onFetchGptRobotAppHandler()
  }
  
  const onUpdateRobotAppIntelligentQAModuleHandler = (params: UpdateRobotAppIntelligentQASettingModel) => {
    return fetchUpdateRobotAppIntelligentQAModule(params).then(isSuccess => {
      if (isSuccess) {
        initChat()
      }
      return isSuccess
    })
  }
  
  const onUpdateIntelligentQAPromptWordHandler = (params: UpdateIntelligentQAPromptWordModel) => {
    return fetchUpdateIntelligentQAPromptWord(params).then(isSuccess => {
      if (isSuccess) {
        initChat()
      }
      return isSuccess
    })
  }
  
  return {
    initialize,
    loading,
    robotId,
    allOutsideModules,
    gptRobotApp,
    robotAppId,
    onFetchGptRobotAppHandler,
    onUpdateIntelligentQAPromptWordHandler,
    onUpdateRobotAppIntelligentQAModuleHandler,
    fetchUpdateRobotAppIntelligentQAModuleSuccess,
  }
}

/**
 * @description 获取 小宝 GPT 机器人 应用
 */
function useFetchSettingGPTRobotApp() {
  
  const { loading, setLoading, clearLoading } = useLoading()
  
  let gptRobotApp: Ref<AIChatRobotAppItemComponentType> = ref(DefaultRobotAppItem) as Ref<AIChatRobotAppItemComponentType>
  
  const fetchGptRobotApp = (params: GetGPTRobotAppModel) => {
    
    setLoading()
    
    return (
      getRobotApp(params).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          const data = MsgModel.getData<SettingGPTRobotAppItem>(result, {} as SettingGPTRobotAppItem)
          
          gptRobotApp.value = transformRobotAppServerItemToComponentItem(data)
          
        } else {
          message.error(MsgModel.getMessage(result))
        }
        
      }).finally(() => {
        clearLoading()
      })
    )
  }
  
  return {
    loading,
    fetchGptRobotApp,
    gptRobotApp
  }
}


function useSettingGPTRobotAppEditStatus() {
  
  const query = computed(() => {
    return qs.parse(window.location.search.substr(1));
  })
  
  const robotId = computed(() => {
    return (query.value.robotId as string) || '';
  });
  
  const robotAppId = computed(() => {
    return (query.value.robotAppId as string) || '';
  });
  
  return {
    robotId,
    robotAppId
  }
}

/**
 * @description 更新 小宝 GPT 机器人 应用 设置
 */
function useFetchSettingGPTRobotAppUpdateSetting() {
  
  const { loading, setLoading, clearLoading } = useLoading()
  
  const isSuccess: Ref<boolean> = ref(false)
  
  const fetchUpdateGptRobotAppSetting = (params: UpdateRobotAppSettingModel) => {
    
    setLoading()
    
    return (
      updateRobotAppSetting(params).then(result => {
        
        isSuccess.value = MsgModel.isSuccess(result)
        
        if (isSuccess.value) {
          // 
        } else {
          message.error(MsgModel.getMessage(result))
        }
        
      }).finally(() => {
        clearLoading()
      })
    )
  }
  
  return {
    loading,
    isSuccess,
    fetchUpdateGptRobotAppSetting
  }
}

/**
 * @description 更新 小宝 GPT 机器人 应用 智能问答 能力设置
 */
function useFetchUpdateRobotAppIntelligentQAModule() {
  
  const { loading, setLoading, clearLoading } = useLoading()
  
  const isSuccess: Ref<boolean> = ref(false)
  
  const fetchUpdateRobotAppIntelligentQAModule = (params: UpdateRobotAppIntelligentQASettingModel) => {
    
    setLoading()
    
    return (
      updateRobotAppIntelligentQAModule(params).then(result => {
        
        isSuccess.value = MsgModel.isSuccess(result)
        
        if (isSuccess.value) {
          // 
        } else {
          message.error(MsgModel.getMessage(result))
        }
        
        return isSuccess.value
        
      }).finally(() => {
        clearLoading()
      })
    )
  }
  
  return {
    loading,
    isSuccess,
    fetchUpdateRobotAppIntelligentQAModule
  }
}

/**
 * @description 更新 小宝 GPT 机器人 应用 智能问答 提示词列表
 */
function useFetchUpdateIntelligentQAPromptWord() {
  
  const { loading, setLoading, clearLoading } = useLoading()
  
  const isSuccess: Ref<boolean> = ref(false)
  
  const fetchUpdateIntelligentQAPromptWord = (params: UpdateIntelligentQAPromptWordModel) => {
    
    setLoading()
    
    return (
      updateIntelligentQAPromptWord(params).then(result => {
        
        isSuccess.value = MsgModel.isSuccess(result)
        
        if (isSuccess.value) {
          // 
        } else {
          message.error(MsgModel.getMessage(result))
        }
        
        return isSuccess.value
        
      }).finally(() => {
        clearLoading()
      })
    )
  }
  
  return {
    loading,
    isSuccess,
    fetchUpdateIntelligentQAPromptWord
  }
}

/** 
 * @description 机器人应用 智能问答设置 选项卡
*/
function useSettingGPTRobotAppIntelligentQATab() {
  
  const active: Ref<SettingGPTRobotAppIntelligentQASettingTypeEnum> = ref(SettingGPTRobotAppIntelligentQASettingTypeEnum.AbilitySetting)
  
  const setActive = (type: SettingGPTRobotAppIntelligentQASettingTypeEnum) => {
    active.value = type
  }
  
  const isActive = (type: SettingGPTRobotAppIntelligentQASettingTypeEnum) => {
    return active.value === type
  }
  
  const isAbilitySettingActive = computed(() => {
    return isActive(SettingGPTRobotAppIntelligentQASettingTypeEnum.AbilitySetting)
  })
  
  const isPromptWordActive = computed(() => {
    return isActive(SettingGPTRobotAppIntelligentQASettingTypeEnum.PromptWord)
  })
  
  const resetActive = () => {
    setActive(SettingGPTRobotAppIntelligentQASettingTypeEnum.AbilitySetting)
  }
  
  return {
    active,
    setActive,
    resetActive,
    isAbilitySettingActive,
    isPromptWordActive,
  }
}


export {
  useSettingGPTRobotAppIntelligentQA,
  useSettingGPTRobotAppIntelligentQATab,
  useFetchSettingGPTRobotAppUpdateSetting,
  useFetchSettingGPTRobotApp,
  useSettingGPTRobotAppEditStatus
}
